package com.freshman_registration.service.impl;

import com.freshman_registration.mapper.NoticeMapper;
import com.freshman_registration.pojo.Notice;
import com.freshman_registration.service.NoticeService;
import com.freshman_registration.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 添加公告
     * @param notice 公告实体对象
     * @return 包含操作结果的Result对象
     */
    @Override
    public Result<?> insertNotice(Notice notice) {
        // 验证必要字段
        if (notice.getPublisherId() == null) {
            return Result.fail("发布者ID不能为空");
        }

        if (notice.getTitle() == null || notice.getTitle().trim().isEmpty()) {
            return Result.fail("公告标题不能为空");
        }

        if (notice.getContent() == null || notice.getContent().trim().isEmpty()) {
            return Result.fail("公告内容不能为空");
        }

        // 设置发布时间为当前时间

        int insert = noticeMapper.insertNotice(notice);
        if (insert > 0) {
            return Result.success("公告添加成功");
        }
        return Result.fail("公告添加失败");
    }
    /**
     * 更新公告
     * @param notice 公告实体对象
     * @return 包含操作结果的Result对象
     */
    @Override
    public Result<?> updateNotice(Notice notice) {
        // 检查公告是否存在
        Notice existingNotice = noticeMapper.queryNoticeById(notice.getId());
        if (existingNotice == null) {
            return Result.fail("公告不存在");
        }

        // 更新发布时间为当前时间
        notice.setPublishTime(new Date());
        int update = noticeMapper.updateNotice(notice);
        if (update > 0) {
            return Result.success("公告更新成功");
        }
        return Result.fail("公告更新失败");
    }

    @Override
    public Result<?> getNoticeStudentList() {
        List<Notice> noticeList = noticeMapper.queryNoticeStudentList();
        return Result.success(noticeList);
    }
    /**
     * 根据ID查询公告
     * @param id 公告ID
     * @return 包含公告信息的Result对象
     */
    @Override
    public Result<?> getNoticeById(Integer id) {
        Notice notice = noticeMapper.queryNoticeById(id);
        if (notice != null) {
            return Result.success(notice);
        }
        return Result.fail("公告不存在");
    }
    /*管理模糊
     * */
    @Override
    public List<Notice> queryNotices(String title, String content, Integer publisherId, Integer isShow, String workNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("title", title);
        params.put("content", content);
        params.put("publisherId", publisherId);
        params.put("isShow", isShow);
        params.put("workNo", workNo);
        return noticeMapper.selectNotices(params);
    }
    /*
     *
     * */
    @Override
    public List<Notice> queryPublishedNotices(String title, String content) {
        Map<String, Object> params = new HashMap<>();
        params.put("title", title);
        params.put("content", content);
        // 强制只查已发布的
        params.put("isShow", 1);
        return noticeMapper.selectNotices(params);
    }

    /**
     * 查询公告列表
     * @return 包含公告列表的Result对象
     */
    @Override
    public Result<?> getNoticeList() {
        List<Notice> noticeList = noticeMapper.queryNoticeList();
        return Result.success(noticeList);
    }

    /**
     * 删除公告
     * @param id 公告ID
     * @return 包含操作结果的Result对象
     */
    @Override
    public Result<?> deleteNotice(Integer id) {
        // 检查公告是否存在
        Notice notice = noticeMapper.queryNoticeById(id);
        if (notice == null) {
            return Result.fail("公告不存在");
        }

        int delete = noticeMapper.deleteNotice(id);
        if (delete > 0) {
            return Result.success("公告删除成功");
        }
        return Result.fail("公告删除失败");
    }
}
